package com.ipzoe.mem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.google.common.collect.Lists;
import com.ipzoe.common.exception.CustomException;
import com.ipzoe.mem.domain.MemberCard;
import com.ipzoe.mem.domain.MemberImg;
import com.ipzoe.mem.domain.MemberRechargePackage;
import com.ipzoe.mem.service.IMemberCardService;
import com.ipzoe.mem.service.IMemberImgService;
import com.ipzoe.mem.service.IMemberRechargePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipzoe.mem.mapper.MemberMapper;
import com.ipzoe.mem.domain.Member;
import com.ipzoe.mem.service.IMemberService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 会员Service业务层处理
 *
 * @author eric
 * @date 2020-08-20
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private IMemberImgService iMemberImgService;

    /**
     * 生成会员号
     * <p>
     *     话务系统，输入会员卡号后7位
     *     CP+YYMM(会员首次购买年月)+M/F性别+YY(出生年份)+【0-4】00000编号
     *      CF+YYMM(会员首次购买年月)+M/F性别+YY(出生年份)+【5-9】00000编号
     *      付费会员，会员卡编号 5位数字，第一位 0-4
     *      分销商代理销售的付费会员，会员卡编号5位 数字，第一位5-9之间
     * </p>
     * @return
     */
    @Override
    public String generateMemberNum(boolean agentFlag, Date birthday, Integer sex){
        StringBuilder memberNumSb = new StringBuilder(15);
        LambdaQueryWrapper<Member> lqw;
        Date today = new Date();
        memberNumSb.append(agentFlag ? "CF" : "CP");
        memberNumSb.append(String.valueOf(DateUtil.format(today, "yyyyMM")).substring(2));
        memberNumSb.append(sex==null ? "N" : (sex == 0 ? "M" : "F"));
        StringBuilder memberNumLastSb;
        do{
            memberNumLastSb = new StringBuilder(7);
            memberNumLastSb.append(birthday==null ? "00" : String.valueOf(DateUtil.year(birthday)).substring(2));
            memberNumLastSb.append(agentFlag ? String.valueOf(RandomUtil.randomInt(5,9)) : String.valueOf(RandomUtil.randomInt(0,4)));
            memberNumLastSb.append(RandomUtil.randomNumbers(4));
            lqw = new LambdaQueryWrapper<>();
            lqw.eq(Member::getMemberNumLast, memberNumLastSb.toString());
        }while(getOne(lqw) != null);

        memberNumSb.append(memberNumLastSb);
        return memberNumSb.toString();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(Member member, String[] memImages, String[] certImages) {

        updateById(member);

        LambdaQueryWrapper<MemberImg> memImgLqw = new LambdaQueryWrapper<>();
        memImgLqw.eq(MemberImg::getMemberId, member.getId());
        iMemberImgService.remove(memImgLqw);

        List<MemberImg> insertImgList = Lists.newArrayList();
        if(ArrayUtil.isNotEmpty(memImages)){
            MemberImg memberImg = null;
            for (String memImage : memImages) {
                memberImg = new MemberImg()
                        .setImgUrl(memImage)
                        .setMemberId(member.getId())
                        .setType(2)
                        .setCreateBy(member.getUpdateBy())
                        .setDelFlag(0);
                insertImgList.add(memberImg);
            }
        }
        if(ArrayUtil.isNotEmpty(certImages)){
            MemberImg memberImg = null;
            for (String memImage : certImages) {
                memberImg = new MemberImg()
                        .setImgUrl(memImage)
                        .setMemberId(member.getId())
                        .setType(1)
                        .setCreateBy(member.getUpdateBy())
                        .setDelFlag(0);
                insertImgList.add(memberImg);
            }
        }
        iMemberImgService.saveBatch(insertImgList);

    }
}
